package com.stock.stockana.ui.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.andexert.library.RippleView;
import com.stock.publiclib.utils.StringUtil;
import com.stock.stockana.R;
import com.stock.stockana.StockCenter;
import com.stock.stockana.beans.ResponseBean;
import com.stock.stockana.constants.NetWorkConstants;
import com.stock.stockana.events.network.GetUserInfoEvent;
import com.stock.stockana.events.network.UpdateUserEvent;
import com.stock.stockana.requests.GetUserInfo;
import com.stock.stockana.requests.UpdateUser;
import com.stock.stockana.ui.activities.SettingActivity;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * Created by ljgsonx on 2016/3/4.
 */
public class MotifyUserNameFragment extends Fragment {

    private static final String TAG = "VersionNoticeFragment->";

    public static final int USERNAME_TYPE_PHONE = 100;
    public static final int USERNAME_TYPE_EMAIL = 101;

    TextInputLayout mUserNameTIL;
    TextView mUserNameEt;
    Button mConfirmBtn;
    int mUserNameType = USERNAME_TYPE_PHONE;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.motify_username_layout, container, false);
        mUserNameEt = (EditText) view.findViewById(R.id.userNameEt);
        mConfirmBtn = (Button) view.findViewById(R.id.confirmBtn);
        mUserNameTIL = (TextInputLayout) view.findViewById(R.id.userNameTIL);
        final Bundle bundle = getArguments();
        if (bundle != null) {
            if(bundle.getInt("userNameType") != 0){
                mUserNameType = bundle.getInt("userNameType");
            }
        }
        if (mUserNameType == USERNAME_TYPE_EMAIL) {
            mUserNameTIL.setHint("请输入新的邮箱:");
        }
        mConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = mUserNameEt.getText().toString();
                if (StringUtil.isBlank(userName)) {
                    if (mUserNameType == USERNAME_TYPE_PHONE) {
                        Toast.makeText(getActivity(), "请输入正确的手机号", Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(getActivity(), "请输入正确的邮箱", Toast.LENGTH_LONG).show();
                    }
                }else {
                    UpdateUser updateUser = UpdateUser.build();
                    if (mUserNameType == USERNAME_TYPE_PHONE) {
                        String phoneNo = "";
                        if (bundle != null){
                            phoneNo = bundle.getString("phoneNo");
                        }
                        if(userName.equals(phoneNo)){
                            Toast.makeText(getActivity(), "您输入的手机号与当前手机号一致!", Toast.LENGTH_LONG).show();
                            return;
                        }
                        updateUser.setPhoneNo(userName);
                    } else {
                        updateUser.setEmail(userName);
                    }
                    StockCenter.doReq(updateUser);
                    ((SettingActivity) getActivity()).showProgress("正在修改...");
                }
            }
        });
        return view;
    }

    @Subscribe
    public void onEventMainThread(UpdateUserEvent event) {
        ((SettingActivity) getActivity()).stopProgress();
        ResponseBean rb = event.getmResponseBean();
        if (rb.getRespCode() == NetWorkConstants.RESPONSE_CODE_OK) {
            Toast.makeText(getActivity(), "修改成功", Toast.LENGTH_LONG).show();
        } else if(rb.getRespCode() == NetWorkConstants.RESPONSE_ERROR_PHONEOREMAIL_HASUSED){
            Log.d(TAG, rb.getErrorMsg());
            Toast.makeText(getActivity(), "该手机号或邮箱已被注册使用", Toast.LENGTH_LONG).show();
        } else if(rb.getRespCode() == NetWorkConstants.RESPONSE_ERROR_EMAIL_INVALID){
            Log.d(TAG, rb.getErrorMsg());
            Toast.makeText(getActivity(), "邮箱格式不正确", Toast.LENGTH_LONG).show();
        }else{
            Log.d(TAG, rb.getErrorMsg());
            Toast.makeText(getActivity(), "可能由于网络原因修改失败,请重试!", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }


}
